
import org.jasypt.util.text.AES256TextEncryptor;

public class Main {
    public static void main(String[] args) {
        // 1加密 2解密
        int type =2;
        // 需要加密或解密的字符串
        String text = "";
        extracted(type,text);

    }

    /**
     *
     * @param type 1加密 2解密
     * @param text 需要加密或解密的字符串
     */
    private static void extracted(int type,String text) {
        if (type == 1) {
            String encrypt = encrypt(text);
            System.out.println("加密后: " + encrypt);
            String decrypt = decrypt(encrypt);
            System.out.println("解密后: " + decrypt);
            if (text.equals(decrypt)) {
                System.out.println("加密解密成功");
            }
        }else {
            String decrypt = decrypt(text);
            System.out.println("解密后: " + decrypt);
        }
    }


    /**
     * 加密
     *
     * @param originalText 需要加密的字符串(明文)
     */
    private static String encrypt(String originalText) {
        AES256TextEncryptor encryptor = getAes256TextEncryptor();
        // 加密字符串
        String encryptedText = encryptor.encrypt(originalText);
        // 解密字符串
        String decryptedText = encryptor.decrypt(encryptedText);
        if (!originalText.equals(decryptedText)) {
            new RuntimeException("加密失败,原因解密后的字符串和原字符串不一致");
        }
        return encryptedText;
    }

    /**
     * 解密
     *
     * @param encryptedText 需要解密的字符串(密文)
     * @return 解密后的字符串(明文)
     */
    private static String decrypt(String encryptedText) {
        AES256TextEncryptor encryptor = getAes256TextEncryptor();
        return encryptor.decrypt(encryptedText);

    }

    private static AES256TextEncryptor getAes256TextEncryptor() {
        // 创建一个 AES256TextEncryptor 对象
        AES256TextEncryptor encryptor = new AES256TextEncryptor();
        // 设置加密密钥
        String encryptionPassword = "15689833343";
        encryptor.setPassword(encryptionPassword);
        return encryptor;
    }
}
